import tkinter as tk
from tkinter import messagebox
from ..widgets.character_card import create_character_card
from ..widgets.animated_button import create_animated_button, create_icon_button
from ..styles.colors import BACKGROUNDS, TEXT_COLORS
from ..styles.fonts import get_font

def show_selection_screen(parent, items, on_back, on_add_character, on_show_details, on_delete_character):
    for widget in parent.winfo_children():
        widget.destroy()
    
    main_container = tk.Frame(parent, bg=BACKGROUNDS["main"])
    main_container.pack(fill="both", expand=True)
    
    header_frame = tk.Frame(main_container, bg=BACKGROUNDS["header"], height=80)
    header_frame.pack(fill="x", pady=(0, 20))
    header_frame.pack_propagate(False)
    
    title_frame = tk.Frame(header_frame, bg=BACKGROUNDS["header"])
    title_frame.pack(side="left", padx=20, fill="y")
    
    title_label = tk.Label(
        title_frame,
        text="🗡️ 我的英雄团队",
        fg=TEXT_COLORS["accent"],
        bg=BACKGROUNDS["header"],
        font=get_font("header", "bold")
    )
    title_label.pack(anchor="w")
    
    stats_label = tk.Label(
        title_frame,
        text=f"成员: {len(items)}/4",
        fg=TEXT_COLORS["secondary"],
        bg=BACKGROUNDS["header"]
    )
    stats_label.pack(anchor="w")
    
    button_frame = tk.Frame(header_frame, bg=BACKGROUNDS["header"])
    button_frame.pack(side="right", padx=20, fill="y")
    
    create_animated_button(
        button_frame,
        "返回",
        on_back,
        style="primary",
        width=8
    ).pack(side="left", padx=5)
    
    if len(items) < 4:
        create_icon_button(
            button_frame,
            "添加角色",
            on_add_character,
            "➕",
            "success"
        ).pack(side="left", padx=5)
    
    content_frame = tk.Frame(main_container, bg=BACKGROUNDS["main"])
    content_frame.pack(fill="both", expand=True, padx=20)
    
    if not items:
        empty_frame = tk.Frame(content_frame, bg=BACKGROUNDS["main"])
        empty_frame.pack(expand=True)
        
        empty_label = tk.Label(
            empty_frame,
            text="🎯 收藏馆空空如也...",
            fg=TEXT_COLORS["secondary"],
            bg=BACKGROUNDS["main"],
            font=get_font("subheader")
        )
        empty_label.pack(pady=20)
        
        empty_desc = tk.Label(
            empty_frame,
            text="点击下方的按钮添加第一个英雄吧！",
            fg=TEXT_COLORS["secondary"],
            bg=BACKGROUNDS["main"]
        )
        empty_desc.pack()
        
        if len(items) < 4:
            create_icon_button(
                empty_frame,
                "创建第一个英雄",
                on_add_character,
                "⭐",
                "accent"
            ).pack(pady=20)
    
    else:
        for i, character in enumerate(items):
            card = create_character_card(
                content_frame,
                character,
                on_select=on_show_details,
                on_delete=lambda char: confirm_delete(char, on_delete_character)
            )
            card.pack(fill="x", pady=10, padx=10)
    
    return main_container

def confirm_delete(character, on_delete_callback):
    result = messagebox.askyesno(
        "确认删除",
        f"确定要删除角色 {character['name']} 吗？\n此操作不可撤销！",
        icon="warning"
    )
    
    if result:
        on_delete_callback(character)